/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/psy-testing/code/model"
	"gitee.com/tugoer/psy-testing/code/api/tags"
)

var Spoof = []*mux.Action {
	{
		Name: "Get Spoof",
		Method: "GET",
		Route: "/spoof",
		Params:mux.Parameters {
			{
				In:mux.Query,
				Schema:&getSpoof{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getSpoof{}
			ctx.WarnParse(p)
			ss, err := model.SelectAuthorSpoof(p.Author, p.Order, p.Limit, p.Offset)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.SendJson(ss)
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Spoof},
			Summary:"获得指定作者的恶搞数据列表",
			Description: "如果未提供作者ID, 则获取所有数据列表",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example:[]model.Spoof{{}},
				},
			},
		},
	},
	{
		Name: "Count Spoof",
		Method: "GET",
		Route: "/spoof/count",
		Params:mux.Parameters {
			{
				In:mux.Query,
				Schema:&countSpoof{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &countSpoof{}
			ctx.WarnParse(p)
			total, err := model.CountSpoof(p.Author)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.SendJson(map[string]int{
				"total": total,
			})
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Spoof},
			Summary:"获得指定作者的恶搞数据总数",
			Description: "如果未提供作者ID, 则获取所有数据总数",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example:map[string]int{
						"total": 300,
					},
				},
			},
		},
	},

	{
		Name: "Create Spoof",
		Method: "POST",
		Route: "/spoof",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&model.Spoof{},
				Ignore:[]string{"ID", "Author"},
			},
		},
		Handler: func(ctx *mux.Context) {
			if admin := GetAdminFromSession(ctx); admin != nil {
				p := &model.Spoof{}
				ctx.WarnParse(p)
				ctx.ErrWarning(p.Create(admin.ID))
				ctx.SendJson(map[string]int{
					"id": p.ID,
				})
			} else {
				ctx.MsgWarning("未登陆", "")
			}
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Spoof},
			Summary:"创建恶搞数据",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example:map[string]int{
						"id": 2589,
					},
				},
			},
		},
	},

	{
		Name: "Update Spoof",
		Method: "PUT",
		Route: "/spoof",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&model.Spoof{},
				Ignore:[]string{"Author"},
			},
		},
		Handler: func(ctx *mux.Context) {
			if admin := GetAdminFromSession(ctx); admin != nil {
				p := &model.Spoof{}
				ctx.WarnParse(p)
				ctx.ErrWarning(p.Update(admin.ID))
				ctx.MsgSuccess("修改成功", "")
			} else {
				ctx.MsgWarning("未登陆", "")
			}
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Spoof},
			Summary:"修改恶搞数据",
		},
	},

	{
		Name: "Delete Spoof",
		Method: "DELETE",
		Route: "/spoof",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&delSpoof{},
			},
		},
		Handler: func(ctx *mux.Context) {
			if admin := GetAdminFromSession(ctx); admin != nil {
				p := &delSpoof{}
				ctx.WarnParse(p)
				s := &model.Spoof{ID: p.ID}
				ctx.ErrWarning(s.Delete(admin.ID))
				ctx.MsgSuccess("删除成功", "")
			} else {
				ctx.MsgWarning("未登陆", "")
			}
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Spoof},
			Summary:"删除恶搞数据",
		},
	},
}

type (
	countSpoof struct {
		Author int `desc:"作者ID, 可为空"`
	}
	getSpoof struct {
		Author int `desc:"作者ID"`
		Order string `required:"排序不能为空"`
		Limit int `required:"limit 不能为空" num:"1<=x<=50" num-msg:"limit 必须在1-50"`
		Offset int `required:"offset 不能为空"`
	}

	delSpoof struct {
		ID int `required:"ID 不能为空"`
	}
)